package libra;

import javax.annotation.PostConstruct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import libra.openfeign.fallback.OpenFeignLocalFallback;
import libra.openfeign.fallbackFactory.OpenFeignServiceFallbackFactory;

/**
 * 外部Controller，通过该Controller触发OpenFeign的调用
 * 可以视为相当于是一个功能类似于SpringMVC.DispatcherServlet的请求分发器
 * @author libra
 */
@RestController
public class DispatcherController {
	@Autowired
	OpenFeignLocalFallback localFallback;
	@Autowired
	OpenFeignServiceFallbackFactory serviceFallbackFactory;
	
	/*********************** 测试OpenFeignClientLocal本地服务调用 **************************/
	// 测试服务调用请求
	@RequestMapping("fallback/exec")
	public Object fallback_exec() {
		return localFallback.exec();
	};
	
	// 测试请求超时
	@RequestMapping("fallback/timeout")
	public Object fallback_timeout(){
		return localFallback.timeout();
	};
	
	// 测试请求处理异常
	@RequestMapping("fallback/ex")
	public Object fallback_ex(){
		return localFallback.ex();
	};


	/*********************** 测试OpenFeignClientService微服务调用 **************************/
	// 测试服务调用请求
	@RequestMapping("fallbackFactory/exec")
	public Object fallbackFactory_exec() {
		return serviceFallbackFactory.exec();
	};
	
	// 测试请求超时
	@RequestMapping("fallbackFactory/timeout")
	public Object fallbackFactory_timeout(){
		return serviceFallbackFactory.timeout();
	};
	
	// 测试请求处理异常
	@RequestMapping("fallbackFactory/ex")
	public Object fallbackFactory_ex(){
		return serviceFallbackFactory.ex();
	};




	@PostConstruct
	public void init() {
		System.out.println( "OpenFeignLocalFallback的注入实例类型：" + localFallback.getClass() );
		System.out.println( "OpenFeignServiceFallbackFactory的注入实例类型：" + serviceFallbackFactory.getClass() );
	}
}
